// -------------------------------------------------------------------------
//    @FileName			:    SampleDialog.cpp
//    @Author           :    Johance
//    @Date             :    2016-12-28
//    @Module           :    SampleDialog
//
// -------------------------------------------------------------------------

#include "SampleDialog.h"
#include "NFComm/NFNetService/NetService.h"

SampleDialog::SampleDialog()
{
}

SampleDialog::~SampleDialog()
{
}

bool SampleDialog::initLayout()
{	
	if(!NFCUIDialog::initLayout())
		return false;
		
    GetObjectByName(&m_pComboBox, "m_pComboBox");
    GetObjectByName(&m_pListView, "m_pListView");
    GetObjectByName(&m_pInputText, "m_pInputText");
    
    
	return true;
}

void SampleDialog::initData(void *customData)
{
    NFCUtils::CreateuiListModel(m_pListView);
    std::vector<std::string> vecOption = { "Option1", "Option2", "Option3" };

    auto addItemFun = [=](int nIndex)
    {
        m_pListView->pushBackDefaultItem();
        ui::Widget *pItem = *m_pListView->getItems().rbegin();
        ui::Text *pName = dynamic_cast<ui::Text *>(pItem->getChildByName("Name"));
        ui::CheckBox *pCheck = dynamic_cast<ui::CheckBox *>(pItem->getChildByName("Check"));
        ui::Widget *pSelected = dynamic_cast<ui::Widget *>(((Node*)pItem)->getChildByName("Selected"));
        pSelected->setVisible(false);
        pCheck->setSelected(false);

        NFCUtils::CreateuiCombo(m_pContent, pName, vecOption);
        pName->setString(vecOption[nIndex]);
        pName->setTag(nIndex);
    };

    m_pListView->addCCSEventListener([=](Ref*, int type)
    {
        if (type == ui::LISTVIEW_ONSELECTEDITEM_END)
        {
            static ui::Widget *pLastSelected = nullptr;
            ui::Widget *pItem = m_pListView->getItem(m_pListView->getCurSelectedIndex());

            if (m_pListView->getIndex(pLastSelected) == -1)
            {
                pLastSelected = nullptr;
            }

            static ui::Widget *pSelected = nullptr;
            static ui::CheckBox *pCheck = nullptr; 
            if (pLastSelected)
            {
                pSelected->setVisible(false);
                pCheck->setSelected(false);
            }

            pSelected = dynamic_cast<ui::Widget *>(((Node*)pItem)->getChildByName("Selected"));
            pCheck = dynamic_cast<ui::CheckBox *>(((Node*)pItem)->getChildByName("Check"));

            pSelected->setVisible(true);
            pCheck->setSelected(true);

            pLastSelected = pItem;
        }
    });

    NFCUtils::CreateuiCombo(m_pContent, m_pComboBox, vecOption, [=](int nIndex)
    {
        addItemFun(nIndex);
    });

    NFCUtils::CreateuiTextField(m_pInputText, "Please Input");

}